//
//  JMLoanApplySuccess.m
//  NewiOSProject
//
//  Created by apple on 2023/9/19.
//  Copyright © 2023 范智渊. All rights reserved.
//

#import "JMLoanApplySuccess.h"
#import "ZZStarView.h"
#import "BRPlaceholderTextView.h"
#import <StoreKit/StoreKit.h>
@interface JMLoanApplySuccess ()
{
    BRPlaceholderTextView *textV;
}
@property (nonatomic , strong) NSString   *grade;

@end
    
@implementation JMLoanApplySuccess
-(void)backBtnClick{
    [self.navigationController popToRootViewControllerAnimated:YES];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.TitleLabel.text = @"Solicitud de pedido";
    self.grade = @"0";
    UIImageView *successImgV = [[UIImageView alloc]initWithFrame:(CGRectMake(0, NAVIGATION_BAR_HEIGHT()+83, 70, 70))];
    [self.view addSubview:successImgV];
    successImgV.image = ImageNamed(@"Slice 3");
    successImgV.centerX = kAppW()/2;
    
    UILabel *titleLab = [JMControl createLabelWithFrame:(CGRectMake(15, successImgV.bottom+20, kAppW()-30, 21)) Font:mediumFont(15) Text:@"Se ha enviado la solicitud de préstamo" color:Hex_Str_COLOR(@"#9B9B9B") textAlignment:1 numberOfLine:0];
    [self.view addSubview:titleLab];
    
    
    UILabel *tipsLab = [JMControl createLabelWithFrame:(CGRectMake(24, titleLab.bottom+12, kAppW()-48, 100)) Font:Font(14) Text:@"Su solicitud está en revisión, el resultado estará disponible dentro de las 24 horas, puede verlo en la aplicación" color:Hex_Str_COLOR(@"#9B9B9B") textAlignment:1 numberOfLine:0];
    [self.view addSubview:tipsLab];
    NSMutableAttributedString *attr = [[NSMutableAttributedString alloc]initWithString:tipsLab.text];
    attr.yy_lineSpacing = 7;
    tipsLab.attributedText = attr;
    
    
    [JMControl changeTextFontWithLabel:tipsLab changeStr:@"Su solicitud de préstamo ha sido presentada" font:fontWithWeight(16, UIFontWeightMedium)];
    [JMControl changeTextColorWithLabel:tipsLab changeStr:@"Su solicitud de préstamo ha sido presentada" color:Text_COLOR_LEVEL1()];
    
    
    UIButton *agreeBtn = [JMControl createButtonWithFrame:(CGRectMake(24, tipsLab.bottom+50, kAppW()-48, 56)) Target:self Action:@selector(backBtnClick) Title:@"OK" titlColor:Hex_Str_COLOR(@"#FEFFCC") font:BoldFont(17) tag:1 backgroundColor:mainGradientColor(CGSizeMake(kAppW()-48,56))];
    [self.view addSubview:agreeBtn];
    agreeBtn.cornerRadius = 28;
    
    UIButton * delegateBtn = [JMControl createButtonWithFrame:(CGRectMake(0, kAppH()-kSafeAreaBottomHeight()-87, kAppW(), 37)) Target:self Action:@selector(delegateBtnClick) Title:@"《 Politica de privacidad 》" titlColor:Hex_Str_COLOR(@"#3C57F8") font:Font(12) tag:1 backgroundColor:UIColor.clearColor];
    [self.view addSubview:delegateBtn];
    delayOperation(0.1, ^{
        if (self.showStart) {
            [self showStartView];
        }
    });
}

-(void)showStartView{
//    /api/user/submitRating
    
    @weakify(self)
    JMPublicAlertView *starViewAlert = [JMPublicAlertView showAlertWithFrame:(CGRectMake(0, 0, kAppW()-40, 400)) Title:@"Califique nuestra aplicación" subTitle:nil BottomBtns:@[@"Subir"] Position:JMPublicAlertViewPositionCenter btnDirection:JMPublicAlertViewButtonDirectionVer btnActionBlock:^(NSInteger btnTag) {
        @strongify(self)
        [starViewAlert dismiss];
        [self agreeBtnClick];
    }];
    
    ZZStarView *starView = [[ZZStarView alloc] initWithImage:[UIImage imageNamed:@"start_gray"] selectImage:[UIImage imageNamed:@"start_light"] starWidth:30 starHeight:30 starMargin:8 starCount:5 callBack:^(CGFloat userGrade, CGFloat finalGrade) {
        @strongify(self);
        NSLog(@"用户实际选择分 === %.2f, 最终分 === %.2f", userGrade, finalGrade);
        if (finalGrade==0) {
            starViewAlert.firstBtn.enable = NO;
        }else{
            starViewAlert.firstBtn.enable = YES;
        }
        self.grade = @(finalGrade).stringValue;
        
    }];
    //默认值, 可以不写, 用户可选分值范围是0.5的倍数.(建议在设置分值之前确定此值)
    starView.sublevel = 1;
    //设置分值, 可以不写, 默认显示0分.(self.params是UIViewController在ZZRouter中扩展的属性, 包含了所有参数)
    starView.grade = 0;;
    //最小值, 默认0.5可以不写, 用户可以设置的最低分值.
    starView.miniGrade = 0;
    [starViewAlert addSubview:starView];
    //设置位置, 定位左上角, 宽高为固定写法
    starView.frame = CGRectMake(0, 112, 212, 36);
    starView.centerX = starViewAlert.width/2;
    
    textV = [[BRPlaceholderTextView alloc]initWithFrame:(CGRectMake(20, 164, starViewAlert.width-40, 140))];
    textV.placeholder = @"Por favor califique esta aplicación (opcional)";
    textV.backgroundColor = Hex_Str_COLOR(@"#F4F4F4");
    textV.cornerRadius = 12;
    [starViewAlert addSubview:textV];
    
    starViewAlert.firstBtn.enable = NO;
}
- (void)requestAppStoreReview {
//    if (@available(iOS 10.3, *)) {
//        [SKStoreReviewController requestReview];
//    } else {
//        // 处理iOS版本低于10.3的情况，可以提供一个备用的评分方式，比如打开App Store链接
//        // 例如，使用下面的方式打开App Store页面：
//        NSString *appStoreUrlString = @"https://itunes.apple.com/app/id?mt=8";
//        NSURL *appStoreURL = [NSURL URLWithString:appStoreUrlString];
//        
//        if ([[UIApplication sharedApplication] canOpenURL:appStoreURL]) {
//            [[UIApplication sharedApplication] openURL:appStoreURL];
//        }
//    }
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"itms-apps://itunes.apple.com/app/id1018221712"]];
}
-(void)agreeBtnClick{

    NSString *suggest = textV.textView.text?textV.textView.text:@"";
    NSDictionary *param = @{@"score":self.grade?self.grade:@"0",@"suggest":suggest};
    [JMBaseRequest.sharedInstance requestBasePost:BasePostRequestUrl(@"/api/user/submitRating") params:param success:^(NSDictionary * _Nullable dataDic, NSArray * _Nullable dataArray) {
        
    } fail:^(NSString * _Nonnull errorMsg) {
        
    }];
    if (self.grade.intValue==5) {
        [self requestAppStoreReview];
    }else{
        [self.navigationController popToRootViewControllerAnimated:YES];
    }
}
-(void)delegateBtnClick{
    [JMCommonMethod presentWebviewWithUrl:serviceUrlWith(loginPrivacyAgreement) andTitle:@"Politica de privacidad"];

}

@end
